package com.oa.android.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oa.android.R;
import com.oa.android.adapter.GongdAdapter;
import com.oa.android.adapter.WorkAdapter;
import com.oa.android.entry.Gongdan;
import com.oa.android.entry.Work;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;
import com.oa.android.view.XListView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 工单
 * Created by zouqh on 2017/7/26.
 */

public class GongdActivity extends Activity implements View.OnClickListener, XListView.IXListViewListener{
    private ImageView close;
    private TextView title;
    private XListView listView;
    private RadioGroup group;
    private int page = 1;
    private int pageSize = 12;
    private int allPage = 0;
    private List<Gongdan> gongdanList;
    ProgressDialog dialog;
    private String keyword = "";
    GongdAdapter adapter;
    private int lx=0;
    public static GongdActivity activity;
    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        setContentView(R.layout.gongd_activity);
        close = (ImageView) findViewById(R.id.close);
        title = (TextView) findViewById(R.id.title_text);
        listView = (XListView) findViewById(R.id.listView);
        group=(RadioGroup)findViewById(R.id.gRadio);
        title.setText("我的工单");
        listView.setPullLoadEnable(true);// 向上滑动加载更多开启
        listView.setPullRefreshEnable(true);
        listView.setXListViewListener(this);
        listView.setFooterName("", false);
        close.setOnClickListener(this);
        gongdanList=new ArrayList<>();
        adapter = new GongdAdapter(this, gongdanList);
        listView.setAdapter(adapter);
        activity=this;
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){

                    case R.id.dcl:
                        lx=0;
                        setRef();
                        break;

                    case R.id.clz:
                        lx=1;
                        setRef();
                        break;

                    case R.id.ycl:
                        lx=2;
                        setRef();
                        break;
                    case R.id.yhf:
                        lx=3;
                        setRef();
                        break;
                }
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(GongdActivity.this,DetailsGongdActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("gd",gongdanList.get(position-1));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        getDate(keyword);
    }



    //获取工作列表列表
    private void getDate(String selstr) {
        dialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.GD_LIST);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("KHXM", selstr);
        params.addBodyParameter("LX", lx+"");
        params.addBodyParameter("DEPID", UConstants.DepId);

        params.addBodyParameter("page", page + "");
        params.addBodyParameter("rows", pageSize + "");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (dowmUp){
                    onLoad();
                }
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        allPage = Integer.parseInt(object.getString("total"));
                        allPage = (int) Math.ceil((double) allPage / pageSize);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<Gongdan>>() {
                            }.getType();
                            List<Gongdan> list = gson.fromJson(object.getString("rows").toString(), lt);
                            if (list != null && list.size() > 0) {
                                gongdanList.addAll(list);
                                adapter.notifyDataSetChanged();
                                if (page == allPage) {
                                    listView.setFooterName("", false);
                                } else if (page <= allPage) {
                                    listView.setFooterVisibility(true);
                                }

                            } else {
                                Toast.makeText(x.app(), "暂无工作安排！", Toast.LENGTH_LONG).show();
                            }

                        } else {
                            Toast.makeText(x.app(), object.getString("msg")+"", Toast.LENGTH_LONG).show();
                        }
//                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void setRef(){
        page=1;
        gongdanList.clear();
        adapter.notifyDataSetChanged();
        getDate(keyword);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (page < allPage) {
                        page++;
                        getDate(keyword);
                    } else {
                        listView.setFooterName("", false);
                    }
                    break;

            }

        }
    };

    boolean dowmUp;
    @Override
    public void onRefresh() {
        dowmUp=true;
        setRef();
    }

    @Override
    public void onLoadMore() {
        handler.sendEmptyMessage(1);
        onLoad();


    }

    private void onLoad() {
        listView.stopRefresh();
        listView.stopLoadMore();
        listView.setRefreshTime("刚刚");
    }

    @Override
    public void onClick(View v) {

        if (v.getId()==R.id.close){
            finish();
        }
    }
}
